package threading.runnable;

import gui.GUIUtils;

import java.io.File;

import objects.data.WorkPacket;


import ssh.SSHUtils;
import utils.Utils;
import utils.ZipUtils;

public class FileOperations implements Runnable {
	private WorkPacket wp;
	private String jobname;
	private String results_dir;
	private String zipname;
	private int op;
	public FileOperations(String jobname, int op)
	{
		this.wp = Utils.userInfo.workPacketMap.get(jobname);
		this.jobname = jobname;
		this.zipname = wp.jobname + "_results.zip";
		this.results_dir = wp.resultDir +File.separator+ zipname;
		this.op = op;
	}
	
	@Override
	public void run() {
		switch(op)
		{
			case 0:
				if(!wp.sendResultsBack) {
					return;
				}
				if(!SSHUtils.ScpFrom(results_dir,"~/"+jobname+"/"+zipname))
				{
					GUIUtils.MainPanel.setMessageLbl("Error: could not retrieve file '"+results_dir+"' from file server.");
					return;
				}
				Utils.out("+ JOB "+jobname+" is now terminated!");
				//JobPanelController.updateJobStatus(Utils.p, jobname, "Terminated");
				if(wp.unzipResults) {
					ZipUtils.unzip(results_dir);
					new File(results_dir).delete();
				}
				break;
			case 1:
				SSHUtils.CmdTo("rm -r"+jobname);break;
			
		}
		
	}

}
